/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bookset.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.bookset.entity.BkPlace;
import com.thinkgem.jeesite.modules.bookset.service.BookPlaceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 设置馆藏和书库Controller
 *
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/bookset/place")
public class BookPlaceController extends BaseController {

    @Autowired
    private BookPlaceService bookPlaceService;

    @ModelAttribute
    public BkPlace get(@RequestParam(required=false) String id) {
        BkPlace bkPlace = null;
        if(StringUtils.isNotBlank(id) && !"null".equals(id)){
            bkPlace = bookPlaceService.get(id);
        }

        if(bkPlace == null){
            bkPlace = new BkPlace();
        }
        return bkPlace;
    }

    /**
     * 查询书库列表
     * @param bkPlace
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("bookset:place:view")
    @RequestMapping(value = {"list", ""})
    public String list(BkPlace bkPlace, HttpServletRequest request, HttpServletResponse response, Model model){
        Page<BkPlace> page = bookPlaceService.findPage(new Page<BkPlace>(request, response), bkPlace);
        model.addAttribute("page", page);
        return "modules/bookset/bookPlaceList";
    }

    @RequiresPermissions("bookset:place:edit")
    @RequestMapping(value = {"form"})
    public String form(BkPlace bkPlace, Model model){
        model.addAttribute("bkPlace", bkPlace);
        return "modules/bookset/bookPlaceForm";
    }

    /**
     * 查询书库列表
     *
     * @param bkPlace
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("bookset:place:batchedit")
    @RequestMapping("batchedit")
    public String batchedit(BkPlace bkPlace, RedirectAttributes redirectAttributes){
        bookPlaceService.batchedit(bkPlace);
        addMessage(redirectAttributes, "批量设置书库成功");
        return "redirect:" + adminPath + "/bookset/place/?repage";
    }

    /**
     * 保存和新增书库
     * @param bkPlace
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("bookset:place:edit")
    @RequestMapping(value = {"save"})
    public String save(BkPlace bkPlace, Model model, RedirectAttributes redirectAttributes){
        if (!beanValidator(model, bkPlace)){
            return form(bkPlace, model);
        }
        bookPlaceService.save(bkPlace);
        addMessage(redirectAttributes, "保存书库'" + bkPlace.getGcdName() + "'成功");
        return "redirect:" + adminPath + "/bookset/place/?repage";
    }

    /**
     * 删除书库
     * @param bkPlace
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("bookset:place:edit")
    @RequestMapping(value = {"delete"})
    public String delete(BkPlace bkPlace, RedirectAttributes redirectAttributes){
        bookPlaceService.delete(bkPlace);
        addMessage(redirectAttributes, "删除书库成功");
        return "redirect:" + adminPath + "/bookset/place/?repage";
    }
}